package padPack
{
	import events.PushButtonSettingsEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	import globals.Primitive;
	import globals.TrueLoop;
	
	import utils.Label;
	import utils.Waveform;
	
	import waveData.LoopData;
	
	public class PadPushButton extends Sprite
	{		
		private var _loop:TrueLoop;
		private var _loopData:LoopData;
		private var _time:int;
		private var _label:Label;
		private var _keybindLabel:Label;
		private var _isRecording:Boolean = false;
		private var _isHit:Boolean = false; //if the key is hit while recorded
		private var _length:int;
		private var _keybindCode:int;
		
		private var _settingsBtn:Sprite;
		
		private var _background:Sprite;
		private var _normalPad:Bitmap;
		private var _activePad:Bitmap;
		
		[Embed(source="assets/pad_04.png", mimeType="image/png")] private const WhitePad:Class;
		[Embed(source="assets/pad_03.png", mimeType="image/png")] private const BluePad:Class;
		
		public function PadPushButton(loop:TrueLoop, length:int)
		{
			_loopData = new LoopData();
			_loop = loop;
			_loop.loopData = _loopData;
			_length = length;
			
			_background = new Sprite();
			addChild(_background);

			_normalPad = new WhitePad();
			_background.addChild(_normalPad);
			_activePad = new BluePad();
			_activePad.visible = false;
			_background.addChild(_activePad);
			
			_loop.addEventListener(TrueLoop.DATA_REFRESH, changeText);
			
			_label = new Label("");
			_keybindLabel = new Label("");
			addChild(_label);
			addChild(_keybindLabel);
			
			
			_settingsBtn = new Sprite();
			_settingsBtn.graphics.beginFill(0x000000, 0.01);
			_settingsBtn.graphics.drawRect(0, 0, 145, 145);
			_settingsBtn.graphics.endFill();
			_settingsBtn.visible = false;
			
			paint();
			
			buttonMode = true;
			addChild(_settingsBtn);
			_settingsBtn.addEventListener(MouseEvent.CLICK, settingsClick);
			function settingsClick(event:MouseEvent):void
			{
				dispatchEvent(new PushButtonSettingsEvent(_loop, PushButtonSettingsEvent.CLICKED));
			}
			
			_background.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			_background.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		
		private function paint():void
		{
			changeText();
		}
		
		private function changeText(event:Event = null):void
		{
			_label.text = _loop.name;
			_label.x = _label.y = 10;
		}
		
		protected function mouseDown(event:MouseEvent = null):void
		{
			_activePad.visible = true;
			trace("mouse down");
			if (_isRecording)
			{
				_isHit = true;
				_loopData.add(getTimer() - _time, 1);
			}
			_loop.dispatchEvent(new Event(TrueLoop.START_PLAYING));
		}
		
		protected function mouseUp(event:MouseEvent):void
		{
			_activePad.visible = false;
			if (_isRecording)
			{
				_loopData.add(getTimer() - _time, 0);
			}
			_loop.dispatchEvent(new Event(TrueLoop.STOP_PLAYING));
		}
		
		public function setEditMode(bool:Boolean):void
		{
			_settingsBtn.visible = bool;
		}
	}
}